package com.bookstore.bauhinia.evaluation.controller;

import com.bookstore.bauhinia.evaluation.service.EvaluationService;
import com.bookstore.bauhinia.pojo.Evaluation;
import com.bookstore.bauhinia.pojo.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EvaluationController {
    @Resource
    private EvaluationService evaluationService;

    @PostMapping("/evaluation/insert")
    public CommonResult insertEvaluation(@RequestBody Evaluation record){
        int result = evaluationService.insert(record);
        System.out.println("*****插入结果:" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功",record);
        } else {
            return new CommonResult(444,"插入数据库失败",null);
        }
    }
    @GetMapping("/evaluation/get/{id}")
    public CommonResult getEvaluationById(@PathVariable("id") int id){
        Evaluation evaluation = evaluationService.selectByPrimaryKey(id);
        System.out.println("*****查询结果:" + evaluation);
        if (evaluation != null) {
            return new CommonResult(200, "查询成功",evaluation);
        } else {
            return new CommonResult(444,"没有对应记录,查询id:" + id,null);
        }
    }
    @GetMapping("/evaluation/delete/{id}")
    public CommonResult deleteEvaluationById(@PathVariable("id") int id){
        int result = evaluationService.deleteByPrimaryKey(id);
        if (result > 0) {
            return new CommonResult(200, "删除数据成功");
        } else {
            return new CommonResult(444,"删除数据失败");
        }
    }
    @PostMapping("/evaluation/update")
    public CommonResult updateEvaluation(@RequestBody Evaluation record){
        int result = evaluationService.updateByPrimaryKey(record);
        if (result > 0) {
            return new CommonResult(200, "更新数据成功",record);
        } else {
            return new CommonResult(444,"更新数据失败",null);
        }
    }

    @GetMapping("/evaluation/all")
    public CommonResult getAllEvaluations(){
        List<Evaluation> evaluations = evaluationService.getAllEvaluations();
        if(evaluations != null){
            return new CommonResult(200, "查询数据成功", evaluations);
        } else{
            return new CommonResult(444, "查询数据失败", null);
        }
    }

    @GetMapping("/evaluation/getByBookId/{bookId}")
    public CommonResult getEvaluationsByBookId(@PathVariable Integer bookId){
        List<Evaluation> evaluations = evaluationService.getEvaluationsByBookId(bookId);
        if(evaluations != null){
            return new CommonResult(200, "查询数据成功", evaluations);
        } else{
            return new CommonResult(444, "查询数据失败", null);
        }
    }
}
